package com.ruitu.health_1.ui.fragment.order;

import android.content.Context;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.github.jdsjlzx.recyclerview.LRecyclerView;
import com.ruitu.arad.Arad;
import com.ruitu.arad.base.base_list.ListBaseAdapter;
import com.ruitu.arad.base.base_list.SuperViewHolder;
import com.ruitu.arad.support.widget.progress.ProgressLayout;
import com.ruitu.health_1.R;
import com.ruitu.health_1.dao.bean.GoodsInfo;
import com.ruitu.health_1.dao.bean.OrderItemList;
import com.ruitu.health_1.dao.bean.StoreOrderItemList;
import com.ruitu.health_1.mvp.model.OrderModel;
import com.ruitu.health_1.mvp.presenter.OrderPresenter;
import com.ruitu.health_1.view.NoCacheLoadingFragment;
import com.ruitu.router_module.AppHolder;

import java.util.List;

public class OrderOverFragment extends NoCacheLoadingFragment<OrderPresenter.OrderP, OrderModel.OrderM> {
    public static OrderOverFragment newInstance() {

        Bundle args = new Bundle();

        OrderOverFragment fragment = new OrderOverFragment();
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    protected int getLayoutRes() {
        return R.layout.fragment_order_list;
    }

    @Override
    protected void initViews(View v) {

    }

    private void getDataList() {
        p.orderList(1, size, AppHolder.instance().getUser().getId(), "5");
    }

    @Override
    protected ProgressLayout findProgressLayout(View v) {
        return null;
    }

    @Override
    protected LRecyclerView findRecyclerView(View v) {
        return null;
    }

    private StoreDataAdapter adapter;

    @Override
    protected RecyclerView.Adapter initAdapter() {
        adapter = new StoreDataAdapter(getContext());
        return adapter;
    }

    @Override
    protected void reqFirst() {
        size = 10;
        getDataList();
    }

    private int size = 10;

    @Override
    protected void reqMore() {
        size = size + 10;
        getDataList();
    }

    @Override
    public void setData(List orderLists) {
        adapter.setDataList(orderLists);
    }

    class StoreDataAdapter extends ListBaseAdapter<StoreOrderItemList> {
        public StoreDataAdapter(Context context) {
            super(context);
        }

        @Override
        public int getLayoutId() {
            return R.layout.item_store_order;
        }

        @Override
        public void onBindItemHolder(SuperViewHolder holder, int position) {
            RecyclerView recyclerview = holder.getView(R.id.l_recyclerview);
            TextView tv_store_name = holder.getView(R.id.tv_store_name);
            final StoreOrderItemList orderItemList = mDataList.get(position);
            tv_store_name.setText(orderItemList.getName());
            ChildDataAdapter childDataAdapter = new ChildDataAdapter(mContext);
            LinearLayoutManager manager = new LinearLayoutManager(mContext);

            recyclerview.setLayoutManager(manager);
            recyclerview.setAdapter(childDataAdapter);
            childDataAdapter.setDataList(orderItemList.getOrderItemList());

        }
    }


    class ChildDataAdapter extends ListBaseAdapter<OrderItemList> {

        public ChildDataAdapter(Context context) {
            super(context);
        }

        @Override
        public int getLayoutId() {
            return R.layout.item_child_order;
        }

        @Override
        public void onBindItemHolder(SuperViewHolder holder, int position) {
            ImageView iv_commodity = holder.getView(R.id.iv_commodity);
            TextView tv_commodity_name = holder.getView(R.id.tv_commodity_name);
            TextView tv_commodity_info = holder.getView(R.id.tv_commodity_info);
            TextView tv_commodity_price = holder.getView(R.id.tv_commodity_price);
            TextView tv_commodity_num = holder.getView(R.id.tv_commodity_num);
            OrderItemList listBean = mDataList.get(position);
            Glide.with(mContext).load(listBean.getProductPic()).apply(Arad.opts).into(iv_commodity);

            tv_commodity_name.setText(listBean.getProductName());
//            tv_commodity_num.setText(listBean.getRealAmount());
            tv_commodity_price.setText(listBean.getRealAmount());
            StringBuffer buffer = new StringBuffer();
            GoodsInfo good = listBean.getGood();
            if (good != null) {
                if (!TextUtils.isEmpty(good.getSp1())) {
                    buffer.append(good.getSp1());
                }
                if (!TextUtils.isEmpty(good.getSp2())) {
                    buffer.append("," + good.getSp2());
                }
                if (!TextUtils.isEmpty(good.getSp3())) {
                    buffer.append("," + good.getSp3());
                }
            }
            tv_commodity_info.setText(buffer.toString());
        }
    }
}
